/**
 *
 */
package co.fitstart.payment.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericJdbcSupport;

import co.fitstart.payment.entity.transaction.TransactionLog;
import co.fitstart.payment.entity.transaction.TransactionRecord;
import co.fitstart.payment.entity.transaction.TransactionStatus;

/**
 * @author lute
 *
 */
@Repository("transactionRecordDaoSupport")
public class TransactionRecordDaoSupport {

    @Autowired
    private GenericJdbcSupport genericJdbcSupport;

    /**
     * Load transaction record by id
     *
     * @param id
     * @return
     */
    public TransactionRecord loadTransactionRecord(Long id) {
        String sentence = "SELECT id AS id, app_id AS app_id, channel_id AS channel_id, amount AS amount, order_no AS order_no, request_no AS request_no, trade_no AS trade_no, card_no AS card_no, transaction_status AS trasaction_status_as_int, last_modified_time AS last_modified_time FROM transaction_record WHERE id = :id";
        List<TransactionRecord> transactionRecords = genericJdbcSupport.queryForList(sentence, "id", id, TransactionRecord.class);
        return CollectionUtils.isEmpty(transactionRecords) ? null : transactionRecords.get(0);
    }

    /**
     * Get transaction record according to request number
     *
     * @param requestNo
     * @return
     */
    public TransactionRecord getTransactionRecord(String requestNo) {
        String sentence = "SELECT id AS id, app_id AS app_id, channel_id AS channel_id, amount AS amount, order_no AS order_no, request_no AS request_no, trade_no AS trade_no, card_no AS card_no, transaction_status AS transaction_status_as_int, last_modified_time AS last_modified_time FROM transaction_record WHERE request_no = :requestNo";
        List<TransactionRecord> result = genericJdbcSupport.queryForList(sentence, "requestNo", requestNo, TransactionRecord.class);
        return CollectionUtils.isNotEmpty(result) ? result.get(0) : null;
    }

    /**
     * Change transaction record status
     *
     * @param transactionRecord
     * @param transactionStatus
     */
    public void changeTransactionRecordStatus(TransactionRecord transactionRecord, TransactionStatus transactionStatus) {
        // Change transaction record
        Date modifiedTime = new Date();
        String sentence = "UPDATE transaction_record SET transaction_status = :transactionStatusAsInt, last_modified_time = :lastModifiedTime WHERE id = :id";
        genericJdbcSupport.executeSQL(sentence, transactionRecord.transactionStatus(transactionStatus).lasModifiedTime(modifiedTime));

        // Create transaction log
        createTransactionLog(new TransactionLog(transactionRecord, transactionStatus.getOperationType()));
    }

    /**
     * Create transaction log
     *
     * @param transactionLog
     */
    public void createTransactionLog(TransactionLog transactionLog) {
        String sentence = "INSERT INTO transaction_log (transaction_record_id, operation_type, modified_time) VALUES (:transactionRecordId, :operationTypeAsInt, :modifiedTime)";
        genericJdbcSupport.executeSQL(sentence, transactionLog);
    }

}
